# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2009  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import math


class Connection(QGraphicsItem):
	
	def __init__(self, _parent, _child):
		QGraphicsItem.__init__(self)
		
		self._parent = _parent
		self._child = _child
		
		self.setParentItem(self._parent)
		
		self.padding = 20
		self.height = 20
		
		self.penWidth = 1
		
		self.color = QColor(200, 100, 100)
		
		self.angle = 0
		
		self.arrowSize = 40
		
		self.calcGeometry()
		
		self._parent.addConnection(self)
		self._parent.addChild(self._child)
		self._child.setParentItem(self._parent)
		self._child.addConnection(self)
		self._child.addParent(self._parent)
		
		self.setToolTip("This is a test")
		
	def __del__(self):
		self._parent.removeConnection(self)
		self._child.removeConnection(self)
		
	def calcGeometry(self):
		if self._child.pos() == QPointF(0,0):
			self.width = self.arrowSize
			self.rect = QRectF(-self.width/2, -self.height/2, self.width, self.height)
			self.prepareGeometryChange()
			return
			
		vector = self._child.pos()
		if vector.x() != 0:
			angle = math.atan(vector.y() / vector.x())
		elif vector.y() > 0:
			angle = math.pi/2
		else:
			angle = -math.pi/2
		dist = math.sqrt(vector.x()**2 + vector.y()**2)
		
		cornerParent = math.atan(self._parent.height/self._parent.width)
		cornerChild = math.atan(self._child.height/self._child.width)
		if angle < cornerParent and angle > -cornerParent:
			factorParent = abs(self._parent.width / 2 / math.cos(angle))
			fpVect = vector * (self._parent.width / 2 / vector.x())
		else:
			factorParent = abs(self._parent.height / 2 / math.sin(angle))
			fpVect = vector * (self._parent.height / 2 / vector.y())
			if angle > -math.pi/2 and angle < 0:
				fpVect = -fpVect
		if angle < cornerChild and angle > -cornerChild:
			factorChild = abs(self._child.width / 2 / math.cos(angle))
			fcVect = vector * (self._child.width / 2 / vector.x())
		else:
			factorChild = abs(self._child.height / 2 / math.sin(angle))
			fcVect = vector * (self._child.height / 2 / vector.y())
			if angle > -math.pi/2 and angle < 0:
				fcVect = -fcVect
		if angle < math.pi/2 and angle > 0:
			fcVect.setY(-fcVect.y())
			fpVect.setY(-fpVect.y())
		if (angle > 0 and vector.x() < 0) or (angle < 0 and vector.y() > 0):
			fcVect = -fcVect
			fpVect = -fpVect
			
		
		self.width = dist-self.padding*2-factorChild-factorParent
		if self.width < self.arrowSize:
			self.width = self.arrowSize
		
		self.rect = QRectF(-self.width/2, -self.height/2, self.width, self.height)
		self.prepareGeometryChange()
		
		if vector.x() < 0:
			angle += math.pi
		between = vector - fpVect - fcVect
		self.setPos(between/2+fpVect)
		self.rotate(angle*180/math.pi - self.angle)
		self.angle = angle*180/math.pi
		
	def boundingRect(self):
		arrow = self.arrowSize * 1.414
		return self.rect.adjusted(-self.penWidth/2, -self.penWidth/2-arrow, self.penWidth/2+10, self.penWidth/2+arrow)
	
	def paint(self, painter, option, widget):
		painter.setRenderHint(QPainter.Antialiasing, True)
		
		brush = QBrush(self.color)
		painter.setBrush(brush)
		painter.setPen(self.color)
		
		painter.drawRect(self.rect)
		painter.translate(self.width/2+10, 0)
		painter.rotate(45)
		painter.drawRect(-self.arrowSize, 0, self.arrowSize, self.arrowSize/2.5)
		painter.drawRect(-self.arrowSize/2.5, 0, self.arrowSize/2.5, self.arrowSize)